﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleDataTools
{
    static public class ConfigUtilty
    {
        static public void Save(object obj, string file)
        {
            if (obj != null)
            {
                File.WriteAllText(file, JsonConvert.SerializeObject(obj, Formatting.Indented));
            }
        }

        static public T Load<T>(string file) where T : class
        {
            if (File.Exists(file))
            {
                return JsonConvert.DeserializeObject<T>(File.ReadAllText(file));
            }
            else
            {
                return null;
            }
        }
    }

    public class GlobalConfig
    {
        public string LastConfigFile { get; set; }
    }

    /// <summary>
    /// 整体配置
    /// </summary>
    public class Config
    {
        [DisplayName("接收数据项配置")]
        public ItemConfig[] Items { get; set; }

        [Browsable(false)]
        public ChartGroupConfig ChartGroupConfig { get; set; }

        [DisplayName("数值监控配置")]
        public ValueItemConfig[] ValueItemConfigs { get; set; }

        [DisplayName("阈值监控配置")]
        public ThresholdValueItemConfig[] ThresholdValueItemConfigs { get; set; }

        public void Save(string file)
        {
            ConfigUtilty.Save(this, file);
        }

        public void Load(string file)
        {
            if (File.Exists(file))
            {
                var temp = JsonConvert.DeserializeObject<Config>(File.ReadAllText(file));
                if (temp.Items != null)
                {
                    Items = temp.Items;
                }
                else
                {
                    Items = new ItemConfig[0];
                }
            }
        }

        public static Config LoadFile(string file)
        {
            return ConfigUtilty.Load<Config>(file);
        }

        internal Config Clone()
        {
            return new Config()
            {
                Items = Items?.Select(x => x.Clone()).ToArray(),
                ChartGroupConfig = ChartGroupConfig.Clone(),
                ThresholdValueItemConfigs = ThresholdValueItemConfigs?.Select(x => x.Clone()).ToArray(),
                ValueItemConfigs = ValueItemConfigs?.Select(x => x.Clone()).ToArray()
            };
        }
    }

    public class ItemConfig
    {
        [DisplayName("数据项名称")]
        public string Name { get; set; }
        [DisplayName("监听UDP端口")]
        public int Port { get; set; }
        [DisplayName("数据项内容数量")]
        public int Count { get; set; }
        [DisplayName("数据类型")]
        public DataType Type { get; set; }
        [DisplayName("保存文件名")]
        public string WriteFileName { get; set; }
        [DisplayName("保存文件方式")]
        public WriteType WriteType { get; set; }
        [DisplayName("数据项格式化方式")]
        public DataFormat DataFormat { get; set; }

        public double GetValue(byte[] buff, int index)
        {
            double value = 0.0;
            if (buff != null && buff.Length > index * (Type == DataType.Double ? 8 : 4))
            {
                value = Type == DataType.Double ?
                                BitConverter.ToDouble(buff, index * 8) :
                                BitConverter.ToSingle(buff, index * 4);
            }
            return value;
        }

        public ItemConfig Clone()
        {
            return new ItemConfig()
            {
                Name = Name,
                Port = Port,
                Count = Count,
                Type = Type,
                WriteFileName = WriteFileName,
                WriteType = WriteType,
                DataFormat = DataFormat
            };
        }
    }

    public enum DataFormat
    {
        E,
        F
    }

    public enum WriteType
    {
        ACSII,
        Byte
    }

    public enum DataType
    {
        Double,
        Float
    }


    public class ChartGroupConfig
    {
        public ChartConfig[] Items { get; set; }

        public void Save(string file)
        {
            File.WriteAllText(file, JsonConvert.SerializeObject(this));
        }

        public void Load(string file)
        {
            if (File.Exists(file))
            {
                var temp = JsonConvert.DeserializeObject<ChartGroupConfig>(File.ReadAllText(file));
                if (temp.Items != null)
                {
                    Items = temp.Items;
                }
                else
                {
                    Items = new ChartConfig[0];
                }
            }
        }

        public ChartGroupConfig Clone()
        {
            return new ChartGroupConfig()
            {
                Items = Items.Select(x => x.Clone()).ToArray()
            };
        }
    }

    /// <summary>
    /// 图表配置
    /// </summary>
    public class ChartConfig
    {
        [DisplayName("曲线控件名称")]
        public string Name { get; set; }
        [DisplayName("曲线配置")]
        public ChartItemConfig[] Items { get; set; }
        [DisplayName("数据项索引")]
        public int DataIndex { get; set; }

        public ChartConfig Clone()
        {
            return new ChartConfig()
            {
                Items = Items.Select(x => new ChartItemConfig() { Index = x.Index, Title = x.Title }).ToArray()
            };
        }
    }

    public class ChartItemConfig
    {
        [DisplayName("数据索引")]
        public int Index { get; set; }
        [DisplayName("曲线名称")]
        public String Title { get; set; }
    }


    /// <summary>
    /// 数据显示配置
    /// </summary>
    public class ValueItemConfig
    {
        /// <summary>
        /// 数据项索引
        /// </summary>
        [DisplayName("数据项索引")]
        public int DataIndex { get; set; }
        /// <summary>
        /// 数据索引
        /// </summary>
        [DisplayName("数据索引")]
        public int Index { get; set; }
        /// <summary>
        /// 数据名称
        /// </summary>
        [DisplayName("数据名称")]
        [DefaultValue("数据名称")]
        public string Name { get; set; }

        public ValueItemConfig Clone()
        {
            return new ValueItemConfig()
            {
                DataIndex = DataIndex,
                Index = Index,
                Name = Name
            };
        }
    }

    /// <summary>
    /// 阈值数据显示配置
    /// </summary>
    public class ThresholdValueItemConfig
    {
        /// <summary>
        /// 数据项索引
        /// </summary>
        [DisplayName("数据项索引")]
        public int DataIndex { get; set; }
        /// <summary>
        /// 数据索引
        /// </summary>
        [DisplayName("数据索引")]
        public int Index { get; set; }
        /// <summary>
        /// 数据名称
        /// </summary>
        [DisplayName("数据名称")]
        [DefaultValue("数据名称")]
        public string Name { get; set; }
        /// <summary>
        /// 大于阈值灯亮
        /// </summary>
        [DisplayName("阈值")]
        [Description("大于阈值灯亮")]
        [DefaultValue(Double.MinValue)]
        public double Threshold { get; set; }

        public ThresholdValueItemConfig Clone()
        {
            return new ThresholdValueItemConfig()
            {
                DataIndex = DataIndex,
                Index = Index,
                Name = Name,
                Threshold = Threshold
            };
        }
    }

}
